package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * 根据给定的用户名删除指定用户
 */
public class Test {
    public static void main(String[] args) {
        try {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入用户名");
            String username = scanner.nextLine();

            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection connection = DriverManager.getConnection(
                    //   jdbc:mysql://数据库IP:端口/数据库名?参数...
                    "jdbc:mysql://localhost:3306/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
                    "root",
                    "root"
            );
            System.out.println("成功连接数据库");
            Statement statement = connection.createStatement();
            /*
                DELETE FROM userinfo
                WHERE username='XXXX'
             */
            String sql = "DELETE FROM userinfo " +
                         "WHERE username='"+username+"'";
            int num = statement.executeUpdate(sql);
            if(num>0){
                System.out.println("删除成功");
            }else{
                System.out.println("删除失败");
            }

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }
}
